$(function () {
	getAllClass();
	getAllSelect();
//	getAllSelect1()
})
function getAllClass() {
	//			存储的id
	//			var id=sessionStorage.getItem("id");
	//			时间戳
	var stamp = new Date().getTime();
	//			存储的token
	var systoken=sessionStorage.getItem("systoken");
	var param = {
		//			id:id,
		stamp: stamp,
		systoken: systoken,
		version: 0,
		visitPlat: 0,
		visitType: 0,
		// visitType: 0,
		isUsed: 0,
	};
	var $banji = $("#banji");
	param = JSON.stringify(param);
	var url = path+"/course/queryClass";
	$.ajax({
		url: url,
		data: {
			param: param
		},
		dataType: 'json',
		type: 'post',
		async: true,
		success: function (data) {
			var rows = data.rows;
			var length = rows.length;
			for (var i = 0; i < length; i++) {
				var $banjiopt = $("<option>" + rows[i].className + "</option>");
				$banji.append($banjiopt)
			}
		}
	})
}

//请求后台返回的课程
function getAllSelect() {
	//			存储的id
	//			var id=sessionStorage.getItem("id");
	//			时间戳
	var stamp = new Date().getTime();
	//			存储的token
	var systoken=sessionStorage.getItem("systoken");
	var param = {
		//			id:id,
		stamp: stamp,
		systoken: systoken,
		version: 0,
		visitPlat: 0,
		visitType: 0,
		// visitType: 0,
		isUsed: 0
	};
	var $kecheng = $("#kecheng");
	// var $kemu = $(".kecheng");
	param = JSON.stringify(param);
	var url= path+"/course/queryAllCourse";
	$.ajax({
		url: url,
		data: {
			param: param
		},
		dataType: 'json',
		type: 'post',
		async: false,
		success: function (data) {
			var rows = data.rows;
			var length = rows.length;
			for (var i = 0; i < length; i++) {
				var $kechengO = $("<option>" + rows[i].name + "</option>");
				$kecheng.append($kechengO);
			}
		}
	})
}
layui.use('laydate', function(){
  var laydate = layui.laydate;
  var laypage = layui.laypage
  //常规用法
  laydate.render({
    elem: '#startDate'
  });
  laydate.render({
    elem: '#endDate'
  });
})

//获取本用户班级
getRecord();
$(".startSearch").click(function() {
	getRecord();
})

function getRecord() {
	var accoutn = $("#personAccount").val();
	var name = $("#name").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();

	layui.use('table', function() {
		var table = layui.table;
		//渲染
		var stamp = new Date().getTime();
		var systoken = sessionStorage.getItem('systoken');
		var param = {
			personAccount: accoutn,
			name: name,
			startDate: startDate,
			endDate: endDate,
			stamp: stamp,
			systoken: systoken,
			version: 0,
			visitPlat: 0,
			visitType: 0,
			isUsed: 0,
		};
		param = JSON.stringify(param);
		var defaultpageSise = 10;
		window.ins1 = _render(defaultpageSise);
		function _render(pageSize) {
		table.render({
			elem: '#test',
			height: 600,
			title: '用户数据表',
			method: 'post',
			url: path + '/record/queryRecord',
			page: true,
			where: {
				param: param
			},
            headers:{
                Authorization:systoken
            },
			autoSort: false,
			//,loading: false
			parseData: function(res) {
				return {
					"code": res.status,
					"msg": res.info,
					"count": res.rows.totalNum,
					"data": res.rows.items
				};
			},
			done: function(res) {
			},
			totalRow: true,
			limit: 10,
			toolbar: '#toolbarDemo',
			//,defaultToolbar: ['filter']
			cols: [
					[{
						field: 'id',
						title: '编号',
						width: 85,
						//								edit: 'text',
						//								sort: true
					},
					{
						field: 'personAccount',
						title: '账号',
//						edit: 'text',
						//								sort: true
					},
					{
						field: 'name',
						title: '姓名',
//						edit: 'text',
						//								sort: true
					},
					{
						field: 'operationType',
						title: '操作状态',
//						edit: 'text',
						width: 200
						//								sort: true
					},
					{
						field: 'content',
						title: '操作内容',
//						edit: 'text',
						//								sort: true
					},
					{
						field: 'createTime',
						title: '创建时间',
					},
					{
						field: 'stamp',
						title: '时间戳'
					}
				]
			],
			page: true
				,
			limits: [1, 5, 10],
			limit:pageSize
				,
			loading: true //请求数据时，是否显示loading
		});
	};
		//工具栏事件
		table.on('toolbar(test)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			switch(obj.event) {
				case 'add':
					layer.msg('添加');
					break;
				case 'update':
					layer.msg('编辑');
					break;
				case 'delete':
					layer.msg('删除');
					break;
				case 'getCheckData':
					var data = checkStatus.data;
					//							      layer.alert(JSON.stringify(data));
					layer.alert("来啊，互相伤害啊！");
					break;
				case 'getCheckLength':
					var data = checkStatus.data;
					layer.msg('选中了：' + data.length + ' 个');
					break;
				case 'isAll':
					layer.msg(checkStatus.isAll ? '全选' : '未全选')
					break;
			};
		});

		//监听表格行点击
		table.on('tr', function(obj) {
		});

		//监听表格复选框选择
		table.on('checkbox(test)', function(obj) {
		});

		//监听表格单选框选择
		table.on('radio(test)', function(obj) {
		});
		//监听表格单选框选择
		table.on('rowDouble(test)', function(obj) {
		});

		//监听单元格编辑
		table.on('edit(test)', function(obj) {

			var value = obj.value //得到修改后的值
				,
				xgdata = obj.data //得到所在行所有键值
				,
				id = xgdata.id //得到id
				,
				field = obj.field; //得到字段
			//						双击单元格改变一个值就可以在这里取得,在这里进行与后台交互,修改信息,

			//编辑单元格
			editTeacher();
			$('.teachername').val("");
			//修改成功与后台交互
			function editTeacher() {

				var stamp = new Date().getTime();
				var version = 0;
				var visit_plat = 0;
				var visit_type = 0;
				var is_used = 0;
				var systoken = sessionStorage.getItem('systoken');
				var param1 = {
					//										obj[field]:value,
					id: id,
					stamp: stamp,
					systoken: systoken,
					version: version,
					visit_plat: visit_plat,
					visit_type: visit_type,
					is_used: is_used
				}
				var param = Object.assign(xgdata, param1);
				param = JSON.stringify(param);
				$.ajax({
					url: "http://192.168.1.107:8083/teacherclass/updateTeacherClass",
					type: "post",
					data: {
						param: param
					},
					dataType: "json",
					success: function() {
						getAllClass();
					}
				})
			}

		});

		//监听行工具事件
		table.on('tool(test)', function(obj) {
			var data = obj.data;
			//						var id = data.id;
			if(obj.event === 'del') {
				layer.confirm('确定删除这一行数据?', function(index) {
					//							这里编写删除的逻辑,下面两个步骤是完成与后台的交互后才执行的
					var id = data.id;
					obj.del();
					layer.close(index);
					//								删除表格
					deleteTeacher();

					function deleteTeacher() {
						var systoken = sessionStorage.getItem('systoken');
						var stamp = new Date().getTime();
						var version = 0;
						var visit_plat = 0;
						var visit_type = 0;
						var is_used = 0;
						//									var systoken=localStorage.getItem(systoken);
						var param = {
							id: id,
							systoken: systoken,
							stamp: stamp,
							version: version,
							visit_plat: visit_plat,
							visit_type: visit_type,
							is_used: is_used,
							//										systoken:systoken
						}
						param = JSON.stringify(param);
						$.ajax({
							url: "http://192.168.1.107:8083/teacher/deleteTeacher",
							type: "post",
							data: {
								param: param
							},
							dataType: "json",
							success: function() {
								getAllClass();
							}
						})
					}
				});
			} else if(obj.event === 'edit') {
				layer.prompt({
					formType: 2,
					value: data.liaison
					//							value: data.experience
				}, function(value, index) {
					obj.update({
						email: value
					});
					layer.close(index);
				});
			}
		});

		//监听排序
		table.on('sort(test)', function(obj) {
			layer.msg('已经完成排序！ ' + obj.field + ' ' + obj.type);
			//服务端排序
			table.reload('test', {
				initSort: obj
					,page: {curr: 1}
					,
				where: {
					key: obj.field
						,
					order: obj.type
				}
			});
		});

		var $ = layui.jquery,
			active = {
				parseTable: function() {
					table.init('parse-table-demo', {
						limit: 3
					});
				},
				add: function() {
					table.addRow('test')
				}
			};
		$('i').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});
		$('.layui-btn').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		})

	});

}
//			查询用户结束
//			删除用户开始
function deleteTeacher() {
	var systoken = sessionStorage.getItem('systoken');
	var stamp = new Date().getTime();
	var version = 0;
	var visit_plat = 0;
	var visit_type = 0;
	var is_used = 0;
	var param = {
		id: id,
		stamp: stamp,
		systoken: systoken,
		version: version,
		visit_plat: visit_plat,
		visit_type: visit_type,
		is_used: is_used
	}
	param = JSON.stringify(param);
	$.ajax({
		url: "",
		type: "post",
		data: {
			param: param
		},
		dataType: "json",
		success: function() {
			//重新查询数据
			getAllClass();
		}

	})
}